package io.xccit.race.service.impl;

import io.xccit.race.mapper.RaceCompetitionMapper;
import io.xccit.race.mapper.RaceUniversityMapper;
import io.xccit.race.mapper.RaceRegionMapper;
import io.xccit.race.mapper.RacePrizeMapper;
import io.xccit.race.service.IDashboardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 数据大屏Service业务层处理
 * 
 * @author ywx
 * @date 2025-10-10
 */
@Service
public class DashboardServiceImpl implements IDashboardService {

    @Autowired
    private RaceCompetitionMapper competitionMapper;

    @Autowired
    private RaceUniversityMapper universityMapper;

    @Autowired
    private RaceRegionMapper regionMapper;

    @Autowired
    private RacePrizeMapper prizeMapper;

    @Override
    public Map<String, Object> getStatistics() {
        Map<String, Object> result = new HashMap<>();
        
        // 获取竞赛总数
        Long competitionCount = competitionMapper.selectCount();
        result.put("totalCompetitions", competitionCount);
        
        // 获取高校总数
        Long universityCount = universityMapper.selectCount();
        result.put("totalUniversities", universityCount);
        
        // 获取地区总数
        Long regionCount = regionMapper.selectCount();
        result.put("totalRegions", regionCount);
        
        // 获取奖品总数
        Long prizeCount = prizeMapper.selectCount();
        result.put("totalPrizes", prizeCount);
        
        return result;
    }

    @Override
    public Map<String, Object> getCompetitionTypeDistribution() {
        Map<String, Object> result = new HashMap<>();
        
        // 从数据库查询真实数据
        List<Map<String, Object>> data = competitionMapper.selectCompetitionTypeDistribution();
        
        result.put("data", data);
        return result;
    }

    @Override
    public Map<String, Object> getUniversityRegionDistribution() {
        Map<String, Object> result = new HashMap<>();
        
        // 从数据库查询真实数据
        List<Map<String, Object>> data = universityMapper.selectUniversityRegionDistribution();
        
        result.put("data", data);
        return result;
    }

    @Override
    public Map<String, Object> getCompetitionTimeDistribution() {
        Map<String, Object> result = new HashMap<>();
        
        // 从数据库查询真实数据
        List<Map<String, Object>> timeData = competitionMapper.selectCompetitionTimeDistribution();
        
        // 构建12个月的数据
        List<String> months = Arrays.asList("1月", "2月", "3月", "4月", "5月", "6月", 
                                           "7月", "8月", "9月", "10月", "11月", "12月");
        List<Integer> values = new ArrayList<>();
        
        // 初始化12个月的数据为0
        for (int i = 0; i < 12; i++) {
            values.add(0);
        }
        
        // 填充实际数据
        for (Map<String, Object> data : timeData) {
            Integer month = (Integer) data.get("month");
            Integer value = ((Number) data.get("value")).intValue();
            if (month != null && month >= 1 && month <= 12) {
                values.set(month - 1, value);
            }
        }
        
        result.put("months", months);
        result.put("values", values);
        return result;
    }

    @Override
    public Map<String, Object> getNationalCompetitionDistribution() {
        Map<String, Object> result = new HashMap<>();
        
        // 从数据库查询真实数据
        List<Map<String, Object>> data = competitionMapper.selectNationalCompetitionDistribution();
        
        result.put("data", data);
        return result;
    }

    @Override
    public Map<String, Object> getStudentRegionDistribution() {
        Map<String, Object> result = new HashMap<>();
        
        // 从数据库查询真实数据
        List<Map<String, Object>> data = universityMapper.selectStudentRegionDistribution();
        
        result.put("data", data);
        return result;
    }

}
